package cn.kgc.crowdfundingadminspringboot.service;

import cn.kgc.crowdfundingadminspringboot.mapper.AdvertisementMapper;
import cn.kgc.crowdfundingadminspringboot.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdvertisementServiceImpl implements AdvertisementService{
    @Resource
    AdvertisementMapper advertisementMapper;
    /**
     * 显示数据加分页和模糊查询
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public PageInfo<Advertisement> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        //startPage(从某页开始，一页显示几行)
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        PageInfo<Advertisement> pageInfo=null;
        if (keyword!=null&&keyword.length()>0){
            AdvertisementExample example = new AdvertisementExample();
            AdvertisementExample.Criteria criteria = example.createCriteria();
            criteria.andNameLike("%"+keyword+"%");
            List<Advertisement> advertisements = advertisementMapper.selectByExample(example);
            pageInfo=new PageInfo<>(advertisements);
        }else{
            List<Advertisement> advertisements = advertisementMapper.selectByExample(null);
            pageInfo=new PageInfo<>(advertisements);
        }
        return pageInfo;
    }

    @Override
    public void saveRole(String name) {
        advertisementMapper.insert(new Advertisement(null,name,name,null,name,null));
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdvertisementExample advertisementExample = new AdvertisementExample();
        AdvertisementExample.Criteria criteria = advertisementExample.createCriteria();
        criteria.andIdIn(adminIdList);
        advertisementMapper.deleteByExample(advertisementExample);
    }

    @Override
    public List<Advertisement> selectById(List<Integer> adminIdList) {
        AdvertisementExample advertisementExample = new AdvertisementExample();
        AdvertisementExample.Criteria criteria = advertisementExample.createCriteria();
        criteria.andIdIn(adminIdList);
        return advertisementMapper.selectByExample(advertisementExample);
    }
}
